<?php

namespace Home\Controller;
use Common\Controller\BaseController;

/**
 * Class FaultRecordsController
 * @package Home\Controller
 * 设备故障记录管理
 */
class FaultRecordsController extends BaseController {

    public function index(){
        
        $type = I('get.type',0,'int');
        $equipment_id = I('get.equ_id',0,'int');
        $records_no = I('get.records_no','','htmlspecialchars');
       
        if (!empty($type)&&empty($records_no)&&!isset($_GET['search'])&&empty($requipment_id)) {
            switch ($type) {
                case '1':
                    $this->detail();
                    break;
                case '2':;
                    # code...
                default:
                    $this->total();
                    break;
            }
        }else{
            
            $this->getIndex($records_no,$equipment_id);
        }
        
    }

    public function getIndex($records_no,$equipment_id=0){
        
        $recordsModel = D('FaultRecords');
        $where = " fault_records.del=1 ";
        
        if(!empty($equipment_id)){
            $where  .= " and fault_records.equipment_id={$equipment_id} ";
            $records_no = '';
        }

        if(!empty($records_no)){
            $where  .= " and locate('{$records_no}',fault_records.records_no) ";
        }
        
        $field = "fault_records.*,equipment.number as equipment_number,equipment.name as equname";
        $list = $recordsModel
            ->field($field)
            ->join('equipment on equipment.equipment_id=fault_records.equipment_id')
            ->where($where)
            ->order('reg_time DESC')
            ->select();
        // echo $recordsModel->_sql();
        // dump($list);
        // exit;
        $this->assign('list',$list);
        $this->assign('records_no',$records_no);
        
        $this->display('index');
    }
    #总体查询
    public function total(){

        $etime = strtotime('today');
        $stime = strtotime(date('Y').'-01-01');
        $recordsModel = D('FaultRecords');
        $equModel = D('Equipment');

        $list = $recordsModel->getTotalFault($stime,$etime);
        $equipment = $equModel->getEquNameAndId();
        
        // // dump($equipment);
        // echo $recordsModel->_sql();
        // dump($list);exit;
        $this->assign('time',array('stime'=>$stime,'etime'=>$etime));
        $this->assign('equipment',$equipment);
        $this->assign('list',$list);
        $this->display('total');
    }
    
    #分设备查询
    public function detail(){

        $equipment_id = I('get.equ_id',0,'int');
        if(empty($equipment_id)){

            $equ_name = I('get.equ_name','','htmlspecialchars');
            $equModel = D('Equipment');
            $where = " equipment.del=1 ";
            if(!empty($equ_name)){
                $where  .= " and locate('{$equ_name}',equipment.name) ";
            }
            $field = "equipment.*,user.name as uname,user.nickname ";
            $list = $equModel
                ->field($field)
               ->join('user on equipment.user_id=user.user_id')
                ->where($where)
                ->order('reg_time DESC')
                ->select();
             // echo $equModel->_sql();
             // dump($list);exit;
            $this->assign('list',$list);
            $this->assign('equ_name',$equ_name);
            $this->display('detail_list');

        }else{

            $recordsModel = D('FaultRecords');
            $this->assign('equipment_id',$equipment_id);
            $this->display('detail');
        }
        

        
    }
    public function getStatistisc(){

        $ret = array('status'=>0,'info'=>'');
        try{
            $equipment_id = I('get.equ_id',0,'int');
            $recordsModel = D('FaultRecords');
            $equipmentModel = D('Equipment');

            $ret['info']['data'] = $recordsModel->getStatistiscByEquId($equipment_id);
            $ret['info']['equipment_info'] = $equipmentModel->getEquById($equipment_id);

        }catch(\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['info']   = $e->getMessage();
        }
       $this->ajaxReturn($ret);
    }
    public function add(){

        $equModel  = D('Equipment');
        $equipment = $equModel->getEquListByUser($this->userid);
        
        $this->assign('equipment',$equipment);
        $this->assign('now',date('Y-m-d',time()));

        $this->display('add');
    }

    public function edit(){

        $recid    = I('get.recid');
        $recordsModel = D('FaultRecords');

        $fault_records = $recordsModel->where('fault_records_id='.intval($recid).' and del=1')->limit(1)->select();

        if(empty($fault_records)){
            $this->error('故障记录不存在');
        }

        $equModel  = D('Equipment');
        $equipment = $equModel->getEquListByUser($this->userid);
        
        $this->assign('equipment',$equipment);
        $this->assign('fault_records',$fault_records[0]);
        $this->display('edit');
    }

    public function save(){

        $ret = array('status'=>200,'msg'=>'','content'=>'');

        if(IS_POST){
            try{

                $equModel = D('FaultRecords');
                $post      = I('post.');
                $res = $equModel->saveFaultRecords($post);
                $ret['status'] = (false==$ret) ? 0 : 200;
                $ret['msg'] = $res['msg'];

            }catch (\Exception $e){

                $ret['status'] = $e->getCode();
                $ret['msg']    = $e->getMessage()." on line ".$e->getLine();
            }
        }else{
            $ret['msg'] = '请求不允许';
        }

        $this->ajaxReturn($ret);
    } 

    public function delete(){

        
        $recid    = I('get.recid');
        $recordsModel = D('FaultRecords');

        $fault_records = $recordsModel->where('fault_records_id='.intval($recid).' and del=1')->limit(1)->select();
    
        if(!empty($fault_records)){
            try{
                $info = array('fault_records_id'=>intval($recid),'del'=>2);
                $res = $recordsModel->setField($info);
                if(false===$res){
                    $ret['status'] = 0;
                    $ret['msg']    = "删除失败";
                }else{
                    $ret['msg']    = "删除成功";
                }

            }catch (\Exception $e){

                $ret['status'] = $e->getCode();
                $ret['msg']    = $e->getMessage()." on line ".$e->getLine();
            }
        }else{
            $ret['msg'] = '设备不存在';
        }
        $this->ajaxReturn($ret);
    }

    public function view(){
        $recid    = I('get.recid');
        $recordsModel = D('FaultRecords');
        
        $field = "fault_records.*,equipment.number as equipment_number,equipment.name as equname";
        $fault_records = $recordsModel
            ->field($field)
            ->join('equipment on equipment.equipment_id=fault_records.equipment_id')
            ->where('fault_records_id='.intval($recid).' and fault_records.del=1')
            ->limit(1)
            ->select();
        // echo $recordsModel->_sql();
        // dump($fault_records);exit;
        if(empty($fault_records)){
            $this->error('设备不存在');
        }

        $this->assign('fault_records',$fault_records[0]);
        $this->display('view');
    }
    
}